package com.xbongbong.sso.help;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.EnableEnum;
import com.xbongbong.sso.domain.entity.OauthCustomParamEntity;
import com.xbongbong.sso.model.OauthCustomParamModel;
import com.xbongbong.sso.pojo.CustomParameterMapPojo;
import com.xbongbong.sso.pojo.OauthCustomParamPojo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 授权自定义参数帮助类
 */
@Component
public class OauthCustomParamHelp {

    private static final Logger LOG = LoggerFactory.getLogger(OauthCustomParamHelp.class);

    @Resource
    private OauthCustomParamModel oauthCustomParamModel;
    public OauthCustomParamPojo oauthCustomParamPojoFormat(OauthCustomParamEntity oauthCustomParam) {
        if (Objects.isNull(oauthCustomParam)) {
            return new OauthCustomParamPojo();
        }
        OauthCustomParamPojo oauthCustomParamPojo = new OauthCustomParamPojo();
        oauthCustomParamPojo.setId(oauthCustomParam.getId());
        oauthCustomParamPojo.setCorpid(oauthCustomParam.getCorpid());
        oauthCustomParamPojo.setConnectorId(oauthCustomParam.getConnectorId());
        oauthCustomParamPojo.setName(oauthCustomParam.getName());
        oauthCustomParamPojo.setAttr(oauthCustomParam.getAttr());
        oauthCustomParamPojo.setValue(oauthCustomParam.getValue());
        JSONObject enableJson = new JSONObject();
        enableJson.put("value", oauthCustomParam.getEnable());
        enableJson.put("text", EnableEnum.getByCode(oauthCustomParam.getEnable()).getMemo());
        oauthCustomParamPojo.setEnable(enableJson);
        oauthCustomParamPojo.setDescription(oauthCustomParam.getDescription());
        oauthCustomParamPojo.setUpdateTime(oauthCustomParam.getUpdateTime());
        oauthCustomParamPojo.setAddTime(oauthCustomParam.getAddTime());
        oauthCustomParamPojo.setDel(oauthCustomParam.getDel());
        return oauthCustomParamPojo;
    }

    /**
     * 获取自定义参数map
     *
     * @param corpid
     * @param connectorId
     * @return
     */
    public CustomParameterMapPojo getCustomParameterMap(String corpid, Long connectorId) {
        Map<String, Object> param = new HashMap<>();
        param.put("corpid", corpid);
        param.put("connectorId", connectorId);
        param.put("enable", EnableEnum.OPEN.getCode());
        param.put("del", DelEnum.NORMAL.getDel());
        List<OauthCustomParamEntity> oauthCustomParams = oauthCustomParamModel.findEntities(param);
        /**
         * 系统字段：auth_code、access_token、identity_authentication、client_id、client_secret、redirect_url
         * 自定义字段：corpid：auth_code、userId："字符串"、sign：SHA256(CONCATENATE(client_id, client_secret))
         *
         * 自定义字段的固定字符串通过map维护好
         */
        Map<String, String> constantCustomParameter = new HashMap<>();

        /**
         * 需要计算的参数map
         * client_id、CONCATENATE(client_id, client_secret)、SHA256(CONCATENATE(client_id, client_secret))、
         * CONCATENATE(client_id, client_secret, "abcd")
         */
        Map<String, OauthCustomParamEntity> calculationCustomParameterMap = new HashMap<>();
        //普通常量处理
        for (OauthCustomParamEntity oauthCustomParam : oauthCustomParams) {
            String attr = oauthCustomParam.getAttr();
            String value = oauthCustomParam.getValue();
            Integer valueType = valueType(value);
            if (Objects.equals(valueType, 1)) {
                constantCustomParameter.put(attr, value);
            } else {
                calculationCustomParameterMap.put(attr, oauthCustomParam);
            }
        }
        CustomParameterMapPojo customParameterMapPojo = new CustomParameterMapPojo();
        customParameterMapPojo.setConstantCustomParameter(constantCustomParameter);
        customParameterMapPojo.setCalculationCustomParameterMap(calculationCustomParameterMap);
        return customParameterMapPojo;
    }

    /**
     * 判断value的类型，1：普通常量、2：替换常量、3：公式
     * @param value
     * @return
     */
    public Integer valueType(String value) {
        /**
         * 存在前括号和后括号
         */
        if (value.indexOf("(") > 0 && value.indexOf(")") > 0) {
            return 3;
        }
        /**
         * 由引号包含起来
         */
        if (value.indexOf("\"") == 0 && value.lastIndexOf("\"") == value.length()-1 && value.lastIndexOf("\"") > 0) {
            return 1;
        }
        /**
         * 其他的认为是替换常量
         */
        return 2;
    }
}
